//
//  RGLoginWithPassViewController.m
//  RedGrass
//
//  Created by wukong on 2018/12/10.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGLoginWithPassViewController.h"
#import "RGLoginWithPassView.h"
#import "RGLoginWithPassViewModel.h"
#import "RGLoginRouter.h"
#import "AppDelegate+APPService.h"

@interface RGLoginWithPassViewController ()<RGLoginWithPassViewDelegate>

@property (strong, nonatomic) RGLoginWithPassView *loginWithPassView;
@property (strong, nonatomic) RGLoginWithPassViewModel *viewModel;

@end

@implementation RGLoginWithPassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.NavStyleType = RGCustomNavStyleType_default;
    [self showBackButtonWithImage:@"返回_登录" target:self selector:@selector(backTo)];
    [self.customContentView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom).mas_offset(34);
        }else{
            make.bottom.mas_equalTo(0);
        }
    }];
    [self.customContentView addSubview:self.loginWithPassView];
    [_loginWithPassView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
}

-(void)backTo{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - viewDelegate

-(void)loginWithPassword:(NSString *)password phone:(NSString *)phone{
    if(phone.length == 0){
        [self showTextMessageWithMessage:@"请输入手机号"];
        return;
    }
    if(password.length == 0){
        [self showTextMessageWithMessage:@"请输入密码"];
        return;
    }
    
    [self.viewModel loginWithPassword:password phone:phone];
}

-(void) resetPasswordWithView:(RGLoginWithPassView *)view phone:(NSString *)phone{
    if(phone.length == 0){
        [self showTextMessageWithMessage:@"请输入手机号"];
        return;
    }
    [RGLoginRouter pushToPasswordResetViewControllerFromVC:self phone:phone];
}


#pragma mark - protocol

-(void) loginWithPasswordSuccess{
    
     [[AppDelegate shareAppDelegate] reInitTabarViewController];
     KPostNotification(KNotificationLoginStateChange, @YES);
}


#pragma mark - getter

-(RGLoginWithPassView *)loginWithPassView{
    if(!_loginWithPassView){
        _loginWithPassView = [[RGLoginWithPassView alloc]init];
        _loginWithPassView.viewDeleagte = self;
    }
    return _loginWithPassView;
}

-(RGLoginWithPassViewModel *)viewModel{
    if(!_viewModel){
        _viewModel = [[RGLoginWithPassViewModel alloc]initWithProtocol:self];
    }
    return _viewModel;
}

@end
